package my.tutorial.javacore.test.analytic;

/**
 * Java Test 5 Threads
 */
class HorizontalThread3 implements Runnable {
	private final int LEFT = 0;
	private final int RIGHT = 1;
	private Robot2 robot;

	public HorizontalThread3(Robot2 robot) {
		this.robot = robot;
	}

	@Override
	public void run() {
		while (true) {
			synchronized (robot) {
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				int decision = (int) Math.round(Math.random());
				switch (decision) {
				case LEFT:
					System.out.println("robot moves left!");
					break;
				case RIGHT:
					System.out.println("robot moves right!");
					break;
				}
			}
		}

	}
}

class VerticalThread4 implements Runnable {
	private final int FORWARD = 0;
	private final int BACKWARD = 1;
	private Robot2 robot;

	public VerticalThread4(Robot2 robot) {
		this.robot = robot;
	}

	@Override
	public void run() {
		while (true) {
			synchronized (robot) {
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				int decision = (int) Math.round(Math.random());
				switch (decision) {
				case FORWARD:
					System.out.println("robot moves forward!");
					break;
				case BACKWARD:
					System.out.println("robot moves backward!");
					break;
				}
			}
		}
	}
}

public class Robot2 {

	public static void main(String[] args) {
		Robot2 robot = new Robot2();
		new Thread(new HorizontalThread3(robot)).start();
		new Thread(new VerticalThread4(robot)).start();
	}
}
